//
//  LPTprescriptionViewController.m
//  LPTClient
//
//  Created by mac on 2018/12/7.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "LPTprescriptionViewController.h"
#import "LPTprescriptionTableViewCell.h"
#import "LPTprescriptionCompTableViewCell.h"
#import "LPTprescriptionEditViewController.h"

@interface LPTprescriptionViewController ()

@property (nonatomic, strong)NSDictionary *dic;

@end

@implementation LPTprescriptionViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initSubViews];
    
    [self requestData];
}

-(void)initSubViews
{
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 44;
    [self.dataSource addObjectsFromArray:@[@[@""],@[@"组成"],@[@"功用"],@[@"主治"]]];
}

-(void)requestData
{
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:[NSString stringWithFormat:@"/api/recipe_detail/%@",self.preId] withBody:@{@"id":self.preId} withMethod:YTKRequestMethodGET];
    [request startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
       
        self.dic = data[@"recipe"];
        [self.tableView reloadData];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
    }];
}

-(NSString *)getCompositionString
{
    NSMutableArray *array = [NSMutableArray array];
    for (NSDictionary *item in self.dic[@"recipe_items"]) {
        
        NSString *subComStr = [NSString stringWithFormat:@"%@（%.1fg）",item[@"chinese_medicine"][@"name"],[item[@"weight"] longValue]/10.0f];
        [array addObject:subComStr];
    }
    
    return [array componentsJoinedByString:@" "];
}

#pragma mark - UITableViewDataSource & Delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *sectionArray = self.dataSource[section];
    return sectionArray.count;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"";
    }
    
    NSArray *sectionArray = self.dataSource[section];
    return [sectionArray firstObject];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.1f;
    }
    
    return 40;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 0.1f;
    }else{
        return UITableViewAutomaticDimension;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section != 0) {
        LPTprescriptionCompTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LPTprescriptionCompTableViewCell" forIndexPath:indexPath];
        
        if (indexPath.section == 1) {
            
            [cell refreshWithContent:[self getCompositionString]];
        }else if (indexPath.section == 2){
            
            [cell refreshWithContent:self.dic[@"effect"]];
        }else if (indexPath.section == 3){
            
            [cell refreshWithContent:self.dic[@"treatment"]];
        }
        
        return cell;
    }else{
        LPTprescriptionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LPTprescriptionTableViewCell" forIndexPath:indexPath];
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"pushToAddPrecriptionVC"]) {
        
        LPTprescriptionEditViewController *vc = segue.destinationViewController;
        vc.dic = self.dic;
        vc.type = kEditPreType;
    }
}

@end
